/**
 * @param {string} s
 * @return {string}
 */
// 数字只可能接左括号
var decodeString = function (s) {
  const stack = []
  let num = '', character = ''
  for (let i = 0; i < s.length; i++) {
    if (s[i] >= '0' && s[i] <= '9') {
      if (character !== '') {
        stack.push(character)
        character = ''
      }
      num += s[i]
    } else if (s[i] >= 'a' && s[i] <= 'z') {
      character += s[i]
      if (i == s.length - 1) stack.push(character)
    } else if (s[i] == '[') {
      if (num !== '') {
        stack.push(parseInt(num))
        num = ''
      }
      stack.push('[')
    } else {
      let tempS = character
      if (character !== '') {
        character = ''
      }
      while (stack.length && stack[stack.length - 1] !== '[') {
        tempS = stack.pop() + tempS
      }
      stack.pop()
      let n = stack.pop()
      let str = ''
      while (n >= 1) {
        str += tempS
        n--
      }
      stack.push(str)
    }
  }
  return stack.join('')
};